package cn.wizzer.app.web.commons.ext.websocket.action.msg.attendant;

import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@IocBean
@NettyAction(uri = "/attendant/open",filters = {TokenFilter.class})
public class OpenAction extends BaseNettyAction {

    @Inject
    @Reference
    private UserService userService;

    /**
     *  是否已选择客服/是否有未读消息
     * @param income
     * @param params
     * @return
     */
    @Override
    public Result action(Channel income, JSONObject params) {
        ChatUserVO targetAttendant = NettyCache.globalGet(NettyCache.localGet(income,"targetAttendant"));
        ChatUserVO self = NettyCache.globalGet(params.get("appid"));
        if(self.isAttendant()){
            Result.breakWith("客服请登录PC端");
        }
        if (targetAttendant == null) {
            //分配客服
            targetAttendant = distribute();
            if(targetAttendant == null){
                NettyMsgUtil.send(income, ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.SEND, self,"当前暂无在线客服，可留言");
            }else{
                NettyCache.localSet(income,"targetAttendant", targetAttendant.getId());
                targetAttendant.getUsers().add(self.getId());
                //通知客服
                Channel channel = targetAttendant.getChannel();
                NettyMsgUtil.send(channel, ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.JOIN, self,null);
            }
        }
        return Result.success(targetAttendant);
    }

    private ChatUserVO distribute() {
        List<User> query = userService.query(Cnd.where("is_online", "=", 1).and("attendant", "=", 1));
        //在线客服聊天人数最少的其中一位
        Optional<ChatUserVO> min = query.stream()
                .map(user -> NettyCache.<ChatUserVO>globalGet(user.getId()))
                .filter(Objects::nonNull)
                .min(Comparator.comparing(ChatUserVO::getChatUserSize));
        if (!min.isPresent()){
            return null;
        }
        return min.get();
    }

}
